﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECI.Entities;
using System.Data;
using ECI.WebLib.Data;
using ECI.CommonLibrary;
using System.IO;

namespace ECI.DynamicData
{
    /// <summary>
    /// 创建程序对应的XML文件
    /// </summary>
    public class XMLCreate
    {
        public  static  void CreateById(string tableGuid)
        {
            string sql = "";
            string pageCode = "";
            string condition = " B.GUID='" + tableGuid + "'";

            //程序
            sql = "SELECT  * FROM ECI_TABLE B WHERE  " + condition;
            DataTable dtTable = DBHelper.GetDataTable(sql);
            dtTable.TableName = "PROGRAM";
            pageCode = dtTable.Rows[0]["CODE"].ToString();
            //元素 
            sql = "SELECT *  FROM ECI_FIELD A,ECI_TABLE B WHERE A.TABLE_GUID = B.GUID AND " + condition;
            DataTable dtField = DBHelper.GetDataTable(sql);
            dtField.TableName = "ELEMENT";
            //功能扩展
            sql = "SELECT * FROM ECI_TABLE_FUNCTION A,ECI_TABLE B WHERE A.TABLE_NAME =B.CODE AND" + condition;
            sql += " ORDER BY A.DISPLAY_SEQ";
            DataTable dtFunction = DBHelper.GetDataTable(sql);
            dtFunction.TableName = "FUNCTION";

            //控件扩展
            sql = "SELECT * FROM ECI_TABLE_CONTROL A,ECI_TABLE B WHERE A.PAGE_CODE =B.CODE AND" + condition;
            sql += " AND A.STATUS='Y'";
            sql += " ORDER BY A.SEQ";
            DataTable dtControl = DBHelper.GetDataTable(sql);
            dtControl.TableName = "CONTROL";
            

            //功能确认
            sql = "SELECT * FROM ECI_TABLE_CONFIRM A,ECI_TABLE B WHERE A.PAGE_CODE =B.CODE AND" + condition;
            DataTable dtConfirm = DBHelper.GetDataTable(sql);
            dtConfirm.TableName = "CONFIRM";

            //数据源
            sql = "SELECT * FROM ECI_TABLE_DATA_SOURCE A,ECI_TABLE B WHERE A.PAGE_CODE =B.CODE AND" + condition;
            DataTable dtDataSource = DBHelper.GetDataTable(sql);
            dtConfirm.TableName = "DATA_SOURCE";

            //数据分组
            sql = "SELECT * FROM ECI_TABLE_GROUP A,ECI_TABLE B WHERE A.PAGE_CODE =B.CODE AND" + condition;
            sql += " ORDER BY A.SEQ";
            DataTable dtGroup = DBHelper.GetDataTable(sql);
            dtGroup.TableName = "GROUP";



            DataSet ds = new DataSet();
            ds.DataSetName = "DEV_FACTORY2.0";
            ds.Merge(dtTable);
            ds.Merge(dtField);
            ds.Merge(dtFunction);
            ds.Merge(dtControl);
            ds.Merge(dtConfirm);
            ds.Merge(dtGroup);


            DirectoryInfo dir = new DirectoryInfo(PageHelper.ServerPath + "\\DEV_FACTORY");
            if (!dir.Exists) dir.Create();
            string fileName = PageHelper.ServerPath + "\\DEV_FACTORY\\" + pageCode + ".xml";
            ds.WriteXml(fileName, XmlWriteMode.WriteSchema);
        }

        public static void CreateAll()
        {
            EciTable eciTable = new EciTable();
            List<EciTable> listTable = eciTable.FetchEntities("ALL");
            foreach (EciTable item in listTable)
            {
                string tableGuid = item.Guid;
                CreateById(tableGuid);
            }
        }
    }
}
